<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\PermissionsRepositories;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PermissionsController extends Controller
{
    public function __construct(PermissionsRepositories $permissionsRepositories)
    {
        $this->permissionsRepositories = $permissionsRepositories;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.permissions.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.permissions.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try
        {
            $this->validate($request,[
                'name'  => 'required|string',
                'slug'  => 'required|string',
            ],[
                'name.required'=>'* 名称必传!',
                'slug.required'=>'*标识必传!',
            ]);
            $ret = $this->permissionsRepositories->save($request->all());

            if($ret)
            {
                return response()->json([
                    'code'=>0,
                    'msg'=>'数据提交成功！',
                    'url'=>route('admin.roles')
                ]);
            }
            return response()->json([
                'code'=>-1,
                'msg'=>'数据提交失败！',
            ]);
        }
        catch (\Exception $exception)
        {
            return response()->json([
                'code'=>-2,
                'msg'=>$exception->getMessage(),
            ]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id,Request $request)
    {


        $info = $this->permissionsRepositories->find($id);;
        //dump($info);
        return view('admin.permissions.edit',compact('info'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        try
        {
            $this->validate($request,[
                'name'  => 'required|string',
                'slug'  => 'required|string',
            ],[
                'name.required'=>'* 名称必传!',
                'slug.required'=>'*标识必传!',
            ]);
            $ret = $this->permissionsRepositories->update($id,$request->all());

            if($ret)
            {
                return response()->json([
                    'code'=>0,
                    'msg'=>'数据提交成功！',
                    'url'=>route('admin.roles')
                ]);
            }
            return response()->json([
                'code'=>-1,
                'msg'=>'数据提交失败！',
            ]);
        }
        catch (\Exception $exception)
        {
            return response()->json([
                'code'=>-2,
                'msg'=>$exception->getMessage(),
            ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        $ids=request()->input('ids');

        if(empty($ids))
        {
            return response()->json([
                'code' => 0,
                'msg'   => '请选择删除项！',
            ]);
        }

        if(!is_array($ids))
        {
            return response()->json([
                'code' => 0,
                'msg'   => '非法参数！',
            ]);
        }

        $ret =$this->permissionsRepositories->delete($ids);
        if($ret)
        {
            return response()->json([
                'code' => 0,
                'msg'   => '删除成功！',
            ]);
        }
        return response()->json([
            'code' => -1,
            'msg'   => '删除失败！',
        ]);
    }
    public function data(Request $request)
    {
        $data = $request->all();
        $res = $this->permissionsRepositories->search($data);
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
}
